package com.easymall.web;

import com.easymall.domain.Category;
import com.easymall.service.CategoryService;
import com.easymall.util.BaseFactory;

import javax.servlet.ServletException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@javax.servlet.annotation.WebServlet(name = "CategoryManageServlet")
public class CategoryManageServlet extends javax.servlet.http.HttpServlet {

    private CategoryService categoryService;

    @Override
    public void init() throws ServletException {
        try {
            categoryService = BaseFactory.getFactory().getInstance(CategoryService.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        try {
            List<Category> categoryList = categoryService.listCategoryAll();
            request.setAttribute("categoryList", categoryList);
        } catch (SQLException e) {
            e.printStackTrace();
            response.sendError(500);
            return;
        }
        request.getRequestDispatcher("/_category_manage.jsp").forward(request, response);

    }
}
